{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "949c71b4",
   "metadata": {},
   "source": [
    "# Python 输入输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0413d968",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print('hello world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cd61ace7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'111'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "dd6ed7ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "233\n",
      "hello 233\n"
     ]
    }
   ],
   "source": [
    "print(f'hello {input()}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3fdc94d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111\n",
      "hello 111,今天是6.25\n"
     ]
    }
   ],
   "source": [
    "print('hello {},今天是{}'.format(input(),6.25))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c2a97e1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A,你好，你今年花了1314元，去过最远的地方是民大\n"
     ]
    }
   ],
   "source": [
    "print('{},你好，你今年花了{}元，去过最远的地方是{}'.format('A',1314,'民大'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8d710377",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11111,你好，你今年花了嘎嘎嘎哈哈元，去过最远的地方是bgbbbbb\n"
     ]
    }
   ],
   "source": [
    "a=11111\n",
    "b='嘎嘎嘎哈哈'\n",
    "c='bgbbbbb'\n",
    "print(f'{a},你好，你今年花了{b}元，去过最远的地方是{c}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "eafbdb94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1111"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=1111\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "3878cd24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a,b=1,2\n",
    "a  # 放上变量名，或者有输出的语句，直接输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "33fbee71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a+=1  # a=a+1\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "2f062f3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c=d=3\n",
    "c,d  # 最后行会给多个变量加括号，并不修改原始数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a3774a69",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['False',\n",
       " 'None',\n",
       " 'True',\n",
       " '__peg_parser__',\n",
       " 'and',\n",
       " 'as',\n",
       " 'assert',\n",
       " 'async',\n",
       " 'await',\n",
       " 'break',\n",
       " 'class',\n",
       " 'continue',\n",
       " 'def',\n",
       " 'del',\n",
       " 'elif',\n",
       " 'else',\n",
       " 'except',\n",
       " 'finally',\n",
       " 'for',\n",
       " 'from',\n",
       " 'global',\n",
       " 'if',\n",
       " 'import',\n",
       " 'in',\n",
       " 'is',\n",
       " 'lambda',\n",
       " 'nonlocal',\n",
       " 'not',\n",
       " 'or',\n",
       " 'pass',\n",
       " 'raise',\n",
       " 'return',\n",
       " 'try',\n",
       " 'while',\n",
       " 'with',\n",
       " 'yield']"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import keyword\n",
    "keyword.kwlist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "17a41d60",
   "metadata": {},
   "outputs": [],
   "source": [
    "sd_K1=1  # 大小写字母，数字（不能放开头），下划线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "6edbac91",
   "metadata": {},
   "outputs": [],
   "source": [
    "fist_name='Wang'\n",
    "FirstName='Wang'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4eca771",
   "metadata": {},
   "source": [
    "# 数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90bd53d9",
   "metadata": {},
   "source": [
    "## 数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "077ca5dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "ed11a538",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(1.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "da5f9179",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(True)\n",
    "type(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "d91a323a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "093b333b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-14"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(-14.9)  # 向0取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "b569c953",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "9e93f351",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "29246f7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "880f3c20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(-77)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40c196fe",
   "metadata": {},
   "source": [
    "## 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "640f31e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type('hhhhhhh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "c341a6b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"111  111111\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "c277996b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type('''\n",
    "1111\n",
    "111\n",
    "''')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ae90a23",
   "metadata": {},
   "source": [
    "### 操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "5f662f4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好同学们'"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"你好\"+\"同学们\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "fa1897e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好你好你好你好你好'"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"你好\"*5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "5ce1cacc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好年轻人'"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'你好同学们'.replace('同学们','年轻人')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "04d35bfe",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'你好同学们'.find('好')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "132db5cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['一', '三四五六七']"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'o(*￣▽￣*)ブ—￣へ￣'.split('—')\n",
    "'王|周|五'.split('|') # 分隔字符串，分割后就没有了\n",
    "str1.split('二')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9f65d69",
   "metadata": {},
   "source": [
    "### 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "be39d691",
   "metadata": {},
   "outputs": [],
   "source": [
    "#     0 1 2 3 4 5 6   ctrl+/\n",
    "str1=\"一二三四五六七\"\n",
    "#    -7-6-5-4-3-2-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "85390b17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str1.find('三')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "9cc10348",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'一三五'"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# str1[]  # 切片：start:stop:step  stop不被包含\n",
    "str1[:4]#         0           1\n",
    "str1[5]# 索引对应文本 \n",
    "str1[0:6:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa4d2569",
   "metadata": {},
   "source": [
    "### 转义符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "9a64d83b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm\n",
      "a\tgirl\n"
     ]
    }
   ],
   "source": [
    "print('I\\'m\\na\\tgirl')  #\\n  \\t  \\\\  \\'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "f9f4f0c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"'\""
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'I\\'m\\na\\tgirl'[1] # 转义字符只占一个字符位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "12db2214",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm a girl\n"
     ]
    }
   ],
   "source": [
    "print(\"I'm a girl\")  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "248ffa67",
   "metadata": {},
   "source": [
    "## 列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cbfb85e",
   "metadata": {},
   "source": [
    "### 创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "f59f6af1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(str1.split('二'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "d8ca2d46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type([1,4,6,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c0b7dd8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "List1=[1,'0',[1,2],'😊'] # 列表无所不包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ce15972a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['两', '只', '黄', '鹂', '鸣', '翠', '柳']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list('两只黄鹂鸣翠柳')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "afa77ca7",
   "metadata": {},
   "outputs": [],
   "source": [
    "List2=list('两只黄鹂鸣翠柳') # 直接把字符串转成列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "642b4aa7",
   "metadata": {},
   "source": [
    "### 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "03e338b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['两', '只', '黄', '鹂', '鸣', '翠', '柳', '--\\\\(˙<>˙)/--']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2.append('--\\(˙<>˙)/--')\n",
    "List2  # 在末尾添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7f557c29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['两', '只', '--\\\\(˙<>˙)/--', '黄', '鹂', '鸣', '翠', '柳']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2.insert(2,'--\\(˙<>˙)/--')\n",
    "List2 # 指定位置添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "47db2250",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['两', '只', '黄', '鹂', '鸣', '翠', '柳']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2.remove('--\\(˙<>˙)/--')\n",
    "List2 # 删除第一个匹配的信息。指定元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ee11f15d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['两', '只', '鹂', '鸣', '翠', '柳']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2.pop(2)\n",
    "List2  # 删除指定位置的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d037e861",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, '只', '鹂', '鸣', '翠', '柳']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2[0]=2\n",
    "List2  # 修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6d0cae57",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2.index('只')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bbad2a6e",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2,\n",
       " '只',\n",
       " '鹂',\n",
       " '鸣',\n",
       " '翠',\n",
       " '柳',\n",
       " 2,\n",
       " '只',\n",
       " '鹂',\n",
       " '鸣',\n",
       " '翠',\n",
       " '柳',\n",
       " 2,\n",
       " '只',\n",
       " '鹂',\n",
       " '鸣',\n",
       " '翠',\n",
       " '柳']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2+List2\n",
    "List2*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "42b29fd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2.count('柳')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "651d7be7",
   "metadata": {},
   "source": [
    "### 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "30525987",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, '只', '鹂', '鸣', '翠']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2[0]\n",
    "List2[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "32a41f46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['柳', '翠', '鸣', '鹂', '只', 2]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List2[::] # 取所有\n",
    "List2[::-1] # 反向取所有 对字符串也适用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc216203",
   "metadata": {},
   "source": [
    "# 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "51fd4116",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, (1, 3))"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1,2,3,(1,3))  #不可修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "7c7961fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, '😊', [1, 23])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1,2,'😊',[1,23])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "2ee98324",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, '只', '鹂', '鸣', '翠', '柳')"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup=tuple(List2)\n",
    "tup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bfe6c68e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(List2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8108b0bb",
   "metadata": {},
   "source": [
    "# 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f72f100e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'&', (1, 2), 1, '😊'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 集合中的元素具有唯一、无序和不可改变\n",
    "{1,'&','😊',(1,2)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "057ef859",
   "metadata": {},
   "outputs": [],
   "source": [
    "set1=set(List2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaef3bb4",
   "metadata": {},
   "source": [
    "## 集合运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "537b1fd7",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({1, 2, 3, 4, 5, 6}, {4, 5, 6, 7, 8, 9})"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1={1,2,3,4,5,6}\n",
    "set2={4,5,6,7,8,9}\n",
    "set1,set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "65b671a3",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{4, 5, 6}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集\n",
    "set1 & set2\n",
    "set1.intersection(set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "dfed9ab5",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7, 8, 9}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并集\n",
    "set1 | set2\n",
    "set1.union(set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6f6a31af",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集\n",
    "set1-set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "8f4736ff",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{7, 8, 9}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set2-set1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c665afbb",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.difference(set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "75a61b6b",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{7, 8, 9}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set2.difference(set1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "3f337a55",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 7, 8, 9}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 异或集合，两个差集的并集\n",
    "set1^set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "abd67125",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 7, 8, 9}"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.symmetric_difference(set2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55e25b8b",
   "metadata": {},
   "source": [
    "## 操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "7563695d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, '😊'}"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.add('😊') # 添加\n",
    "set1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "e194aa4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "set1.remove('😊') # 删除时没有就报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "61f88c45",
   "metadata": {},
   "outputs": [],
   "source": [
    "set1.discard('😊') # 删除时没有就不执行仍和操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "9316b8c7",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7, 8, 9}"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.update(set2) # 把集合2添加到集合1里面\n",
    "set1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6e1eacb",
   "metadata": {},
   "source": [
    "# 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "c2838ee1",
   "metadata": {},
   "outputs": [],
   "source": [
    "Dict2={'姓名':'wang',(1,2):[1,2,3]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "c8f93535",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'姓名': 'kunkun', '爱好': ['唱', '跳', 'RAP', '篮球|']}"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict([['姓名','kunkun'],\n",
    "      ['爱好',['唱','跳','RAP','篮球']]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "27554e99",
   "metadata": {},
   "outputs": [],
   "source": [
    "Dict1=dict([('姓名','kunkun'),\n",
    "      ('爱好',['唱','跳','RAP','篮球'])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "4aebb291",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'键2': '值2', '键1': '值1'}"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict([('键2','值2'),('键1','值1')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "b698cde2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'姓名': 'wang', '爱好': ['唱', '跳', 'RAP', '篮球'], (1, 2): [1, 2, 3]}"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dict1.update(Dict2)\n",
    "Dict1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "23095902",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'姓名': 'wang', '爱好': ['唱', '跳', 'RAP', '篮球'], (1, 2): [1, 2, 3], '名次': 'C位'}"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dict1.update({'名次':'C位'})\n",
    "Dict1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "36be5e54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'姓名': '坤坤', '爱好': ['唱', '跳', 'RAP', '篮球'], (1, 2): [1, 2, 3], '名次': 'C位'}"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dict1['姓名']='坤坤'  # 只可以改值\n",
    "Dict1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "de6693e0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dict1.pop((1, 2)) # 返回删除键对应的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "82ad28e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['姓名', '爱好', '名次'])"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dict1.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "c3bcc127",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['坤坤', ['唱', '跳', 'RAP', '篮球'], 'C位'])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dict1.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "7001110d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('姓名', '坤坤'), ('爱好', ['唱', '跳', 'RAP', '篮球']), ('名次', 'C位')])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dict1.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "459bdf61",
   "metadata": {},
   "source": [
    "# 循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbb10f14",
   "metadata": {},
   "source": [
    "## 对对象进行循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "73708397",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['对', '象', '循', '环']"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List1=list('对象循环')\n",
    "List1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "364df51f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['对对对', '象象象', '循循循', '环环环']"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "New_list=[]\n",
    "for i in List1:\n",
    "    New_list.append(i*3)\n",
    "New_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "91f9ac6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "270"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串\n",
    "str1='12e34e56e78e90'\n",
    "list1=str1.split('e')\n",
    "# 字符串分割，int\n",
    "New_list1=[]\n",
    "for i in list1:\n",
    "    New_list1.append(int(i))\n",
    "sum(New_list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "38c3f06d",
   "metadata": {},
   "outputs": [],
   "source": [
    "ans=0\n",
    "for i in list1:\n",
    "    ans=ans+int(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "b9e4cea0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小李周一打卡', '小李周二打卡', '小李周三打卡', '小李周四打卡', '小李周五打卡']"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list3=['小张周一打卡','小张周二打卡','小张周三打卡','小张周四打卡','小张周五打卡']\n",
    "# 小张，都换成小李\n",
    "New_list3=[]\n",
    "for i in list3:\n",
    "    New_list3.append(i.replace('小张','小李'))\n",
    "New_list3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9feeeb8b",
   "metadata": {},
   "source": [
    "## 对数据进行循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "a78b752f",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):   # start:0,stop,step:1 左闭右开\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "ff66ca7f",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,5):   # start:0,stop,step:1\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "baefc3ec",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,5,2):   # start:0,stop,step:1\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "eac25b53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "**\n",
      "***\n",
      "****\n",
      "*****\n"
     ]
    }
   ],
   "source": [
    "# *\n",
    "# **\n",
    "# ***\n",
    "# ****\n",
    "# *****\n",
    "for i in range(1,6):   # start:0,stop,step:1\n",
    "    print('*'*i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ee80963",
   "metadata": {},
   "source": [
    "## enumerate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "1af1bad6",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 小张周一打卡\n",
      "1 小张周二打卡\n",
      "2 小张周三打卡\n",
      "3 小张周四打卡\n",
      "4 小张周五打卡\n"
     ]
    }
   ],
   "source": [
    "# for i,item in enumerate(list3):\n",
    "for i,item in enumerate(list3):\n",
    "    print(i,item)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9ebcefe",
   "metadata": {},
   "source": [
    "## 双重循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "id": "85ed19b3",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1×1=1\t\n",
      "\n",
      "2×1=2\t2×2=4\t\n",
      "\n",
      "3×1=3\t3×2=6\t3×3=9\t\n",
      "\n",
      "4×1=4\t4×2=8\t4×3=12\t4×4=16\t\n",
      "\n",
      "5×1=5\t5×2=10\t5×3=15\t5×4=20\t5×5=25\t\n",
      "\n",
      "6×1=6\t6×2=12\t6×3=18\t6×4=24\t6×5=30\t6×6=36\t\n",
      "\n",
      "7×1=7\t7×2=14\t7×3=21\t7×4=28\t7×5=35\t7×6=42\t7×7=49\t\n",
      "\n",
      "8×1=8\t8×2=16\t8×3=24\t8×4=32\t8×5=40\t8×6=48\t8×7=56\t8×8=64\t\n",
      "\n",
      "9×1=9\t9×2=18\t9×3=27\t9×4=36\t9×5=45\t9×6=54\t9×7=63\t9×8=72\t9×9=81\t\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(f'{i}×{j}={i*j}',end='\\t')\n",
    "    print('\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "268bff5a",
   "metadata": {},
   "source": [
    "# while"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "b983a20a",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_11764/2044646855.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "while True:  # 死循环\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "id": "5401774c",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你要猜的数：0\n",
      "o(￣┰￣*)ゞ，你猜错了哟\n",
      "请输入你要猜的数：2\n",
      "o(￣┰￣*)ゞ，你猜错了哟\n",
      "请输入你要猜的数：3\n",
      "o(￣┰￣*)ゞ，你猜错了哟\n",
      "请输入你要猜的数：4\n",
      "o(￣┰￣*)ゞ，你猜错了哟\n",
      "请输入你要猜的数：1\n",
      "o(￣┰￣*)ゞ，你猜错了哟\n",
      "请输入你要猜的数：5\n",
      "o(￣┰￣*)ゞ，你猜错了哟\n",
      "请输入你要猜的数：6\n",
      "o(￣┰￣*)ゞ，你猜错了哟\n",
      "请输入你要猜的数：7\n",
      "o(￣┰￣*)ゞ，你猜错了哟\n",
      "请输入你要猜的数：8\n",
      "恭喜！o(*￣▽￣*)ブ，猜对了！就是8\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "num=random.randint(0,10)\n",
    "guess=int(input('请输入你要猜的数：'))\n",
    "while guess!=num:\n",
    "    print('o(￣┰￣*)ゞ，你猜错了哟')\n",
    "    guess=int(input('请输入你要猜的数：'))\n",
    "print(f'恭喜！o(*￣▽￣*)ブ，猜对了！就是{num}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "id": "d6c4ab4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "142\n"
     ]
    }
   ],
   "source": [
    "# 1+2+3+4+……\n",
    "# 一共要加多少个数，才能大于10000\n",
    "num=0\n",
    "var_sum=0\n",
    "while var_sum<=10000:\n",
    "    num+=1\n",
    "    var_sum+=num\n",
    "print(num+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb741ba5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
